package xyz.chenyongpeng.userconsumerdubbo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.chenyongpeng.to.VisitorTo;
import xyz.chenyongpeng.userconsumerdubbo.service.UserService;
import xyz.chenyongpeng.userconsumerdubbo.service.VisitorsService;
import xyz.chenyongpeng.utils.PagesData;
import xyz.chenyongpeng.utils.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Visitors")
public class VisitorsController {

    @Autowired
    private UserService userService;

    @Autowired
    private VisitorsService visitorsService;

    /**
     * 获取一个用户下面的游客信息，默认最后5条
     * @param wx_openid
     * @param limit
     * @return
     */
    @PostMapping("/get")
    public R get(@RequestParam(name = "wx_openid",required = true) String wx_openid,
                 @RequestParam(name = "limit",required = false) String limit){
        if (!StringUtils.isEmpty(wx_openid)){
            return R.error(403, "没有登录");
        }
        String u_id = userService.getUid(wx_openid);

        if (StringUtils.isEmpty(limit)){
            limit = "0,5";
        }
        List<VisitorTo> data = visitorsService.getListByUId(u_id, limit);
        int total = visitorsService.getCountByUId(u_id);
        Map<String,Object> result = new HashMap<>();
        PagesData pagesData = new PagesData(total, data);
        result.put("data", pagesData);
        return R.ok(result);
    }


    /**
     * 添加一名游客。
     * @param wx_openid
     * @param v_id_card
     * @param v_id_type
     * @param v_truename
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestParam(name = "wx_openid",required = true) String wx_openid,
                 @RequestParam(name = "v_id_card",required = true) String v_id_card,
                 @RequestParam(name = "v_id_type",required = true) String v_id_type,
                 @RequestParam(name = "v_truename",required = true) String v_truename){
        if (!StringUtils.isEmpty(wx_openid)){
            return R.error(403, "没有登录");
        }
        if (!StringUtils.isEmpty(v_id_card)){
            return R.error(500, "参数不全");
        }
        if (!StringUtils.isEmpty(v_truename)){
            return R.error(500, "参数不全");
        }
        String u_id = userService.getUid(wx_openid);

        Map<String,Object> parms = new HashMap<>();
        parms.put("u_id", u_id);
        parms.put("v_id_card", v_id_card);
        parms.put("v_id_type", v_id_type);
        parms.put("v_truename", v_truename);
        int v_id = visitorsService.addVisitors(parms);

        Map<String,Object> result = new HashMap<>();
        result.put("data", result);
        return R.ok(result);
    }
}
